<template>
  <div class="app-container">
    <el-form>
      <el-row>
        <el-col :span="3">
          <el-form-item>
            <el-button size="small" @click="handleBack">返回上一页</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item>
            <div style="height: 36px;line-height: 40px"> 
              <span style="font-wight: blod;font-size: 24px">调价管理</span>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-form>
      <el-form-item label="客户名称：">
        <span>{{ customUserName }}</span>
      </el-form-item>
    </el-form>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="调价状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option label="待生效" :value="1"></el-option>
          <!-- <el-option label="生效中" :value="0"></el-option> -->
          <el-option label="历史价" :value="2"></el-option>
          <el-option label="已取消" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label-width="120px"
        label="主标号名称"
        prop="primaryDesignatorName"
      >
        <el-input
          v-model.trim="queryParams.primaryDesignatorName"
          placeholder="请输入主标号名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="附加标号名称"
        label-width="120px"
        prop="additionalTitleName"
      >
        <el-input
          v-model.trim="queryParams.additionalTitleName"
          placeholder="请输入附加标号名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同加价名称" label-width="120px" prop="markupsName">
        <el-input
          v-model.trim="queryParams.markupsName"
          placeholder="请输入合同加价名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="dialogVisible = true"
          v-hasPermi="['system:firm:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:firm:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:firm:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList"  :height="divHeight">
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="价格类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">主标号</span>
          <span v-if="scope.row.type == 2">附加标号</span>
          <span v-if="scope.row.type == 3">合同加价</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="统一社会信用代码" align="center" prop="code" />   -->
      <el-table-column label="计价类型" align="center" prop="valuationType">
        <template slot-scope="scope">
          <span v-if="scope.row.valuationType == 1">单价</span>
          <span v-if="scope.row.valuationType == 2">一口价</span>
        </template>
      </el-table-column>
      <el-table-column label="调整前价格" align="center" prop="previousPrice" />
      <el-table-column label="调整后价格" align="center" prop="afterPrice" />
      <el-table-column label="调价时间" align="center" prop="updateTime" />
      <el-table-column label="生效时间" align="center" prop="takeEffectTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{
            statusOptions.find((item) => item.value == scope.row.status)
              .label || ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" />

      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        fixed="right"
        width="160"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:firm:edit']"
            >调价协议</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status == 1"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:firm:remove']"
            >取消调价</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改我方合同主体公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <div style="display: flex; margin-bottom: 10px">
        <div style="margin-right: 100px">
          <span style="font-weight: bold">名称：</span>
          <span>{{ form.name }}</span>
        </div>
        <div>
          <span style="font-weight: bold">价格类型：</span>
          <span v-if="form.type == 1">主标号</span>
          <span v-if="form.type == 2">附加标号</span>
          <span v-if="form.type == 3">合同加价</span>
        </div>
      </div>
      <div style="display: flex">
        <div style="margin-right: 100px">
          <span style="font-weight: bold">当前价格：</span>
          <span>{{ form.previousPrice }}</span>
        </div>
        <div>
          <span style="font-weight: bold">计价类型：</span>
          <span v-if="form.type == 1">单价</span>
          <span v-if="form.type == 2">一口价</span>
        </div>
      </div>
      <div style="margin: 20px 0;barg"></div>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-position="left"
        label-width="160px"
      >
        <NumberFormItem
          requiredMessage="调整后价格"
          label="调整后价格（元）"
          labelWidth="140px"
          errorTip="价格不能为负数"
          prop="afterPrice"
        >
          <el-input
            type="number"
            v-model="form.afterPrice"
            placeholder="调整后价格（元）"
          />
        </NumberFormItem>
        <el-form-item
          label="生效日期"
          label-width="100px"
          :rules="[
            { required: true, message: '请选择日期', trigger: 'change' },
          ]"
          prop="takeEffectTime"
        >
          <el-date-picker
            :picker-options="{
              disabledDate: disabledDate,
            }"
            v-model="form.takeEffectTime"
            type="datetime"
            placeholder="选择日期"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="上传调价协议"
          label-width="120px"
          :rules="[
            { required: true, message: '上传调价协议', trigger: 'change' },
          ]"
          prop="agreementUrl"
        >
          <FileUpload
            v-model="form.agreementUrl"
            :fileType="['pdf', 'doc', 'docx']"
            :limit="1"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="上传调价协议"
      :visible.sync="fielOpen"
      width="500px"
      append-to-body
    >
      <el-form
        ref="file-form"
        :model="form"
        :rules="rules"
        label-position="left"
        label-width="160px"
      >
        <el-form-item
          label="上传调价协议"
          label-width="120px"
          :rules="[
            { required: true, message: '请上传调价协议', trigger: 'change' },
          ]"
          prop="agreementUrl"
        >
          <FileUpload
            v-model="form.agreementUrl"
            :fileType="['pdf', 'doc', 'docx']"
            :limit="5"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="fielOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <SelectMainOrSubMainOrPumping
      :dialogVisible="dialogVisible"
      @onClose="dialogVisible = false"
      @onSubmit="handleOnSubmit"
    ></SelectMainOrSubMainOrPumping>
  </div>
</template>

<script>
import {
  infoList,
  infoCancel,
  addContractInfo,
  updateContractInfo,
} from "@/api/service/marketing/priceAdjustmentManagement";
import SelectMainOrSubMainOrPumping from "@/components/SelectDialog/SelectMainOrSubMainOrPumping";
import NumberFormItem from "@/components/NumberFormItem";
export default {
  components: {
    NumberFormItem,
    SelectMainOrSubMainOrPumping,
  },
  name: "Firm",
  data() {
    return {
      divHeight:300,
      fielOpen: false,
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我方合同主体公司表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        status: null,
        contractId: null,
        primaryDesignatorName: null,
        additionalTitleName: null,
        markupsName: null,
      },
      customUserName: "",
      // 表单参数
      form: {
        contractId: "",
        type: "",
        valuationType: "",
        relevancyId: "",
        name: "",
        previousPrice: "",
        afterPrice: "",
        takeEffectTime: "",
        agreementUrl: "",
      },
      statusOptions: [
        { value: 1, label: "待生效" },
        { value: 2, label: "生效中" },
        { value: 3, label: "已取消" },
      ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "公司名称不能为空", trigger: "blur" },
        ],
        code: [
          {
            required: true,
            message: "统一社会信用代码不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.divHeight = window.innerHeight - 300;
    //const id = row.id || this.ids
    this.queryParams.contractId = this.$route.query.id;
    this.customUserName = this.$route.query.name;
    this.getList();
  },
  methods: {
    handleBack() {
      this.$router.push({
        path: "/marketing/contract",
      });
    },
    handleOnSubmit(row) {
      this.reset();
      this.form = {
        ...this.form,
        contractId: this.queryParams.contractId,
        relevancyId: row.id,
        name: row.name,
        previousPrice: row.previousPrice,
        valuationType: row.valuationType,
        type: row.type,
      };
      this.handleAdd(row);
    },
    /** 查询我方合同主体公司列表 */
    getList() {
      this.loading = true;
      infoList(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contractId: "",
        type: "",
        valuationType: "",
        relevancyId: "",
        name: "",
        status: "",
        previousPrice: "",
        afterPrice: "",
        takeEffectTime: "",
        agreementUrl: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      //this.reset();
      this.open = true;
      this.title = "新增调价";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.reset();
      this.form = row;
      this.fielOpen = true;

      //   const id = row.id || this.ids
      //   getFirm(id).then(response => {
      //     this.form = row;
      //     this.fielOpen = true;
      //     this.title = "修改调价";
      //   });
    },
    /** 提交按钮 */
    async submitForm() {
      let formValid = this.$refs["form"];
      if (this.fielOpen) {
        formValid = this.$refs["file-form"];
      }

      formValid.validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateContractInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.fielOpen = false;
              this.getList();
            });
          } else {
            addContractInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.fielOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("确定取消调价吗？")
        .then(function () {
          return infoCancel({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/firm/export",
        {
          ...this.queryParams,
        },
        `firm_${new Date().getTime()}.xlsx`
      );
    },
    disabledDate(value) {
      const date = new Date();
      return date.valueOf() > value.getTime();
    },
  },
};
</script>
<style scoped>
.app-container{
  overflow: hidden;
}
</style>
